package com.apes.scm.masterdata.frame.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.frame.model.SalesTerritories;
import com.apes.scm.masterdata.frame.repository.SalesTerritoriesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 功能：公司信息
 *
 * @author HuJianlin
 * @create 2018-03-09 14:35
 */
@Service("salesTerritoriesService")
public class SalesTerritoriesService extends DomainService {

    @Autowired
    public SalesTerritoriesRepository salesTerritoriesRepository;

    public SalesTerritories save(SimpleRequest request) {
        SalesTerritories salesTerritories = request.getO(SalesTerritories.class);
        salesTerritoriesRepository.saveAndFlush(salesTerritories);
        return salesTerritories;
    }

    public SalesTerritories update(SimpleRequest request) {
        SalesTerritories salesTerritories = request.getO(SalesTerritories.class);
        salesTerritoriesRepository.saveAndFlush(salesTerritories);
        return salesTerritories;
    }

    public String delete(SimpleRequest request) {
        SalesTerritories salesTerritories = request.getO(SalesTerritories.class);
        salesTerritoriesRepository.delete(salesTerritories);
        return "删除成功";
    }


    /**
     * 功能：查询
     *
     * @param request
     */
    public Page<SalesTerritories> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        // Sort sort = new Sort(Sort.Direction.DESC, "salesOrg");
        return salesTerritoriesRepository.findAll(JpaDsl.toCriteria(filters), PageRequest.of(request.get("page"), request.get("size")));
    }
}
